﻿//*************************************************************************************************
// ExtractionRuleSelectTag.cs
// Owner: Herodotos Herodotou (t-herohe)
//
// An extraction rule used to extract the selected option from a 'select' html tag
//
// Copyright(c) Microsoft Corporation, 2008
//*************************************************************************************************

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using WebAndLoadTestPluginsAndRules.Properties;

using Microsoft.VisualStudio.TestTools.WebTesting;

namespace WebAndLoadTestPluginsAndRules
{
    [LocalizableDisplayName("ValidationRuleNameSelectStatement")]
    [LocalizableDescription("ValidationRuleDescriptionSelectStatement")]
    public class ExtractionRuleSelectTag : ExtractionRule
    {
        // ****************************************************************************
        // Overriden method
        // ****************************************************************************

        /// <summary>
        /// Extract the selected option of a particular html select tag
        /// </summary>
        public override void Extract(object sender, ExtractionEventArgs e)
        {
            int i = 0;
            foreach (string selected in new SelectTagParser(e.Response.BodyString).GetAllSelectedOptions(SelectTagName))
            {
                if (Index == -1 || Index == i)
                {
                    e.WebTest.Context[this.ContextParameterName] = selected;
                    e.Success = true;
                    return;
                }
                ++i;
            }

            // Unable to find the tag or the option
            e.Success = false;
            if (!e.Response.IsHtml)
            {
                e.Message = string.Format(CultureInfo.InvariantCulture,
                    Resources.ErrorResponseNotHtmlForExtractionRules);
            }
            else
            {
                e.Message = string.Format(CultureInfo.InvariantCulture,
                    Resources.ExtractionErrorSelectTagNotFound, SelectTagName);
            }
        }


        // ****************************************************************************
        // Public Properties
        // ****************************************************************************

        [LocalizableDisplayName("PropertyNameSelectTagName")]
        [LocalizableDescription("PropertyDescriptionSelectTagName")]
        public string SelectTagName
        {
            get { return m_selectTagName; }
            set { m_selectTagName = (value.Length == 0) ? null : value; }
        }

        [LocalizableDisplayName("PropertyNameIndex")]
        [LocalizableDescription("PropertyDescriptionIndex")]
        [DefaultValue(-1)]
        public int Index
        {
            get { return m_index; }
            set { m_index = value; }
        }


        // ****************************************************************************
        // Private Member Variables
        // ****************************************************************************

        private string m_selectTagName;
        private int m_index;
    }
}
